﻿using System.IO;
using uTILLIty.ADExtractor.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.DirectoryServices;
using uTILLIty.ADExtractor.Core.Exporters.Csv;

namespace uTILLIty.ADExtractor.CoreTests
{


	/// <summary>
	///This is a test class for CsvExporterTest and is intended
	///to contain all CsvExporterTest Unit Tests
	///</summary>
	[TestClass]
	public class CsvExporterTest
	{
		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext { get; set; }

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion

		/// <summary>
		///A test for Export
		///</summary>
		[TestMethod]
		public void ExportTest()
		{
			var target = new CsvExporter();
			var extractor = new ActiveDirectoryExtractor();
			var query = new QueryInformation
			{
				Query = "(&(objectCategory=User)(initials=*)(sn=A*))",
				PropertiesToLoad = new[] {
					AdPropertyInfo.Parse("Initials"),
					AdPropertyInfo.Parse("GivenName"), 
					AdPropertyInfo.Parse("sn"), 
					AdPropertyInfo.Parse("DisplayName"), 
					AdPropertyInfo.Parse("sAMAccountName"),
					AdPropertyInfo.Parse("department"),
					AdPropertyInfo.Parse("objectSID")
				}
			};
			SearchResultCollection result = extractor.QueryActiveDirectory(query);

			target.DefaultStream = new MemoryStream();
			target.Export(query, result, new CsvExporterConfiguration { Encoding = "UTF-8", Separator = ";" });
			target.DefaultStream.Flush();
			target.DefaultStream.Seek(0, SeekOrigin.Begin);
			var reader = new StreamReader(target.DefaultStream);
			TestContext.WriteLine(reader.ReadToEnd());
		}

		/// <summary>
		///A test for Export
		///</summary>
		[TestMethod]
		public void ExportSubElementsTest()
		{
			var target = new CsvExporter();
			var extractor = new ActiveDirectoryExtractor();
			var query = new QueryInformation
			{
				Query = "(&(objectCategory=User)(initials=*)(sn=A*))",
				PropertiesToLoad = new[] {
					AdPropertyInfo.Parse("Initials"),
					AdPropertyInfo.Parse("GivenName"), 
					AdPropertyInfo.Parse("Lastname=sn"), 
					AdPropertyInfo.Parse("DisplayName"), 
					AdPropertyInfo.Parse("sAMAccountName"),
					AdPropertyInfo.Parse("memberof[0].name"),
					AdPropertyInfo.Parse("department"),
					AdPropertyInfo.Parse("objectSID"),
					AdPropertyInfo.Parse("ManagerSID=manager.objectSID"),
					AdPropertyInfo.Parse("ManagerLogon=manager.sAMAccountName"),
					AdPropertyInfo.Parse("ManagerName=manager.cn"),
					AdPropertyInfo.Parse("count(manager)"),
					AdPropertyInfo.Parse("Managers:count(manager)"),
					AdPropertyInfo.Parse("Managers:count(manager.objectGuid)"),
				}
			};
			SearchResultCollection result = extractor.QueryActiveDirectory(query);

			target.DefaultStream = new MemoryStream();
			target.Export(query, result, new CsvExporterConfiguration { Encoding = "UTF-8", Separator = ";" });
			target.DefaultStream.Flush();
			target.DefaultStream.Seek(0, SeekOrigin.Begin);
			var reader = new StreamReader(target.DefaultStream);
			TestContext.WriteLine(reader.ReadToEnd());
		}
	}
}
